<template>
  <view class="page-container flex flex-col items-center">
    <view class="h-110"></view>
    <image :src="utils.imgFormat('/profile/image/bg1.png')" class="welcome-bg" mode="aspectFill"></image>
    <view class="h-200"></view>
    <view class="page-body flex flex-col items-center justify-center">
      <view :class="loading?'animate__animated animate__tada':'animate_1'" class="confirm-btn" @click="toPath">
        开始练习
      </view>
    </view>
  </view>
</template>

<script setup>
import {ref} from "vue";
import {onLoad, onUnload,onShareAppMessage} from '@dcloudio/uni-app'
import utils from '@/utils/utils.js'
import {customer} from "@/api";
import useUserStore from '@/store/user.js'
const useStore = useUserStore()
const loading = ref(false)

const toPath = () => {
  if(!loading.value) return
  uni.showLoading({title: '登录中'})
  uni.login({
    provider: 'weixin',
    success:  (loginRes)=> {
      console.log('loginRes:',loginRes)
      customer.getWechatCode({info: {code: loginRes.code}}).then(res => {
        console.log('登录',res)
        if(res.code===0){
          useStore.uid = res.data?.uid || ''
          useStore.userInfo = res.data || {}
          if(useStore.userInfo.avatarUrl){
            uni.showToast({title: '登录成功', icon: 'success', duration: 1500})
            uni.hideLoading()
            setTimeout(()=>{
              uni.switchTab({
                url: '/pages/index/index'
              })
            }, 1000)
          }
          if(useStore.userInfo.avatarUrl) return
          uni.getUserInfo({
            provider: 'weixin',
            success:  (infoRes)=> {
              console.log('infoRes:',infoRes)
              useStore.userInfo = infoRes.userInfo  || {}
              const params = {
                'uid': useStore.uid,
                'nickName': infoRes.userInfo.nickName,
                'gender': infoRes.userInfo.gender,
                'country': infoRes.userInfo.country,
                'province': infoRes.userInfo.province,
                'city': infoRes.userInfo.city,
                'avatarUrl': infoRes.userInfo.avatarUrl,
              }
              console.log('params：', params);
              customer.getWechatUserInfo({info: params}).then(res => {
                if(res.code===0){
                  uni.showToast({title: '登录成功', icon: 'success', duration: 1500})
                  uni.hideLoading()
                  setTimeout(()=>{
                    uni.switchTab({
                      url: '/pages/index/index'
                    })
                  }, 1000)
                }
              })
            },
            fail: (err) => {
              console.log('err:',err)
              uni.showToast({title: '登录成功', icon: 'success', duration: 1500})
              uni.hideLoading()
              setTimeout(()=>{
                uni.switchTab({
                  url: '/pages/index/index'
                })
              }, 1500)
            }
          });
        }
      })
    },
    fail: (err) => {
      uni.showToast({title: '登录失败', icon: 'none'})
      console.log('err:',err)
    }
  })
}
onLoad(() => {
  if (useStore.uid && useStore.userInfo.avatarUrl) {
    uni.switchTab({
      url: '/pages/index/index'
    })
    return
  }
  setTimeout(() => {
    loading.value = true
  }, 1500)
})
onShareAppMessage(() => {
  return {
    title: "低空科技-执照考题宝典",
    path: "/pages/welcome/index",
    imageUrl: utils.imgFormat('/profile/image/bg1.png')
  }
})
</script>

<style lang="scss" scoped>
.welcome-bg {
  width: 240rpx;
  height: 232rpx;
}

.confirm-btn {
  width: 280rpx;
  height: 98rpx;
  line-height: 98rpx;
  background: linear-gradient(to bottom, #034995 0%, #005FC6 100%);
  border-radius: 8rpx;
  text-align: center;
  color: #fff;
  font-size: 36rpx;
}

.animate_1 {
  animation: rise 1.5s ease-out; // 添加动画
}

/* 定义动画 */
@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(500rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

</style>